//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')
const keyborad = require('../../components/modules/keyboard')


function adapterCode(num=4, data =[]) {
  let output = []
  for(let i= 0; i< num; i++) {
    output.push({
      id: 'code-input-'+i,
      type: 'number',
      disabled: true,
      value: data[i] || '',
      bindinput: 'onTap'
    })
  }
  return output
}

Pager({
  data: {
    code: null,
    codeLength: 6,
    codeVal: '',
    isFocus: false,
    codeLength2: 4,
    codeVal2: '',
    isFocus2: false,
    keyboard: null
  },
  $formInst: null,
  onEdit(e, params, inst) {
    if (inst.value('phone').disabled) {
      inst.value('phoneEdit', {
        itemClass: 'item-icon-edit ss-btn-text edit'
      })
      inst.value('phone', {
        readyonly: false,
        disabled: false,
      })
    }
    else {
      inst.value('phoneEdit', {
        itemClass: 'item-icon-edit ss-btn-text'
      })
      inst.value('phone', {
        readyonly: true,
        disabled: true,
      })
    }
  },
  Focus(e){  
    var that = this;  
    var inputValue = e.detail.value;  
    that.setData({  
      codeVal: inputValue,  
    })  
  }, 
  Blur(e){  
    this.setData({
      isFocus: false
    }) 
  }, 
  onTapCodeInput(e) {
    var that = this;  
    that.setData({  
      isFocus: true
    }) 
  },

  Focus2(e){  
    var that = this;  
    that.setData({  
      isFocus2: true
    })
    that.$keyboardInst.show()
  }, 
  onSubmit(e, params, inst) {
    const sss = this.getElementsById('comForm1')
  },
  onShow: function () {},
  onReady: function () {
    const codes = adapterCode(6)
    const that = this
    this.setData({
      code: {
        $id: 'formInst',
        data: [{
          input: [
            {
              id: 'phone',
              type: 'number',
              disabled: true,
              value: '13013333333',
              maxlength: 12
            },
            {
              id: 'phoneEdit',
              type: 'button',
              value: '',
              itemClass: 'item-icon-edit ss-btn-text',
              // inputClass: 'icon-close',
              tap: 'onEdit'
            }
          ]
        }],
        listClass: 'ss-form-edit mb-default'
      },
      keyboard: keyborad({id: 'keyboradId'})
    }, () => {
      that.$formInst = that.getElementsById('formInst')
      that.$keyboardInst = that.getElementsById('keyboradId')
      that.$keyboardInst.hooks.on('onKeyboardItem', function (params) {
        const {key, isLast} = params
        if (!isLast && that.data.codeVal2.length < that.data.codeLength2) {
          that.setData({
            codeVal2: that.data.codeVal2 + key,
            isFocus2: true
          })
        }
        if (isLast && that.data.codeVal2.length > 0) {
          that.setData({
            codeVal2: that.data.codeVal2.substring(0, that.data.codeVal2.length - 1)
          })
        }
      })
      that.$keyboardInst.hooks.on('onKeyboardHide', function() {
        that.setData({
          isFocus2: false
        })
      })
    })
    
  },
  //获取粘贴板内容
  // pasteboard: function (e) {
  // abc: function (e) {
  //   var th = this;
  //   wx.getClipboardData({
  //     success(res) {
  //       var a = res.data;
  //       console.log(a)
  //       // th.setData({
  //       //   kw: a,
  //       //   inputValue: a,
  //       // })
  //     }
  //   })
  // },
  onLoad: function () {},
})
